package com.basic.contraller;

import org.junit.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

public class JedisPoolUtils {


    private static JedisPool pool = null;

    static {
//        加载配置文件
        InputStream in = JedisPoolUtils.class.getClassLoader().getResourceAsStream("redis.properties");
        Properties pro = new Properties();
        try {
            pro.load(in);
        } catch (IOException e) {
            e.printStackTrace();
        }

//        获得连接池配置对象，设置配置锁
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
//        设置最大连接数
        jedisPoolConfig.setMaxTotal(Integer.parseInt(pro.get("redis.MaxTotal").toString()));
//        最大空闲连接数
        jedisPoolConfig.setMaxIdle(Integer.parseInt(pro.get("redis.MaxIdle").toString()));
//        最小空闲个数
        jedisPoolConfig.setMinIdle(Integer.parseInt(pro.get("redis.MinIdle").toString()));
//        获得连接池
        pool = new JedisPool(jedisPoolConfig, pro.getProperty("redis.Url"), Integer.parseInt(pro.getProperty("redis.Port")));
    }

    //    获得jedis资源的方法
    public static Jedis getJedis() {

        return pool.getResource();
    }

    public static void main(String[] args) {
        Jedis jedis = getJedis();
        String Loginkey = "201408121018";
        String notLoginkey = "201408121042";
        jedis.del(Loginkey);
        jedis.del(notLoginkey);
//        jedis.lpush(Loginkey,"1","1000","2","1001","3","1002","4","1003","5","1004");
//        jedis.lpush(notLoginkey, "1", "1002", "2", "1000", "2", "1005", "1", "1003");
        System.out.println("已登录的购物车" + jedis.lrange(Loginkey, 0, -1));
        System.out.println("未登录的购物车" + jedis.lrange(notLoginkey, 0, -1));
        List<String> notLogin = jedis.lrange(notLoginkey, 0, -1);
        if (jedis.lrange(notLoginkey, 0, -1).size() == 0) {
//            return “合并成功”
        }

//        查询未登录购物车中是否有商品 和 已登录购物车中是否有商品
//        如未登录购物车中有商品登录的购物车中无商品则直接将未登录购物车的key sessionid  改为 rediskey
        if (jedis.lrange(Loginkey, 0, -1).size() == 0 && jedis.lrange(notLoginkey, 0, -1).size() > 0) {

//将未登录购物车中的商品添加进已登录购购物车中
            for (int i = notLogin.size() - 1; i >= 0; i = i - 1) {
                String value = notLogin.get(i);
                jedis.lpush(Loginkey, value);
            }
            System.out.println("合并后" + jedis.lrange(Loginkey, 0, -1));

        }
//               n为未登录购物车中商品的数量-1
        int n = notLogin.size() / 2 - 1;
        for (int i = notLogin.size() - 2; i >= 0; i = i - 2) {
            String notLoginSkuId = notLogin.get(i);
            int m = 0;
            List<String> Login = jedis.lrange(Loginkey, 0, -1);
            for (int j = 0; j <= Login.size() - 2; j = j + 2) {
                String LoginSkuId = Login.get(j);
                if (notLoginSkuId.equals(LoginSkuId)) {
                    Integer addre = m * 2 + 1;
                    int number = Integer.parseInt(Login.get(m * 2 + 1)) + Integer.parseInt(notLogin.get(n * 2 + 1));
                    String s = jedis.lset(Loginkey, addre, String.valueOf(number));
                    m++;
                    break;
                }
                if (j == Login.size() - 2) {
                    jedis.lpush(Loginkey, notLogin.get(n * 2 + 1), notLogin.get(n * 2));
                    break;
                }
                m++;
            }
            n--;
        }

        System.out.println("合并后" + jedis.lrange(Loginkey, 0, -1));
    }
@Test
public void   delete () throws  Exception{
    List<Long> skus =new ArrayList<Long>();
    skus.add(10001L);
    skus.add(10002L);
    skus.add(10003L);
    skus.add(10004L);

    for (long sku :skus) {
        System.out.println(String.valueOf(sku));
    }

}
}
